<template>
  <view class="invoice-form">
    <form>

      <view class="form-item">
        <view class="form-item-label"><text>*</text>抬头类型</view>
        <view class="form-item-content">
          <radio-group  @change="radioChange">
            <label><radio :checked="false" :value="1"/>企业</label>
            <label><radio :checked="false" :value="2"/>个人/政府事业单位</label>
          </radio-group>
        </view> 
      </view>

      <view class="form-item">
        <view class="form-item-label"><text>*</text>发票抬头</view>
        <view class="form-item-content">
           <input class="weui-input" v-model="upform.gmF_MC" placeholder="抬头名称"/>
        </view> 
      </view>

      <view class="form-item" v-show="radio != 2">
        <view class="form-item-label"><text>*</text>税号</view>
        <view class="form-item-content">
           <input class="weui-input" v-model="upform.gmF_NSRSBH" placeholder="纳税人识别号"/>
        </view> 
      </view>

      <view class="form-item">
        <view class="form-item-label">单位地址</view>
        <view class="form-item-content">
           <input class="weui-input" v-model="upform.gmF_DZDH" placeholder="单位地址"/>
           <!-- <textarea auto-height placeholder-class="invoice-textarea-place" class="invoice-textarea"  placeholder="单位地址"/> -->
        </view> 
      </view>

      <view class="form-item">
        <view class="form-item-label">电话号码</view>
        <view class="form-item-content">
           <input class="weui-input" v-model="upform.gmF_LXFS"  placeholder="电话号码"/>
        </view> 
      </view>

      <view class="form-item">
        <view class="form-item-label">开户银行</view>
        <view class="form-item-content">
           <input class="weui-input" v-model="khh" placeholder="开户银行"/>
        </view> 
      </view>

      <view class="form-item">
        <view class="form-item-label">银行账号</view>
        <view class="form-item-content">
           <input class="weui-input" v-model="yhzh" placeholder="银行账号"/>
        </view> 
      </view>

      <view class="form-item">
        <view class="form-item-label">备注</view>
        <view class="form-item-content">
           <input class="weui-input" v-model="upform.bz" placeholder="备注"/>
        </view> 
      </view>

      <button class="invoice-submit" :disabled="submittype" type="default" @tap="onSubimt">提交</button>


    </form>
  </view>
</template>
<script>
import Taro from "@tarojs/taro";
import { setGlobalData, getGlobalData } from "../../globalData.js";
import "./invoice.less";
import Empty from "../../components/empty/empty";
import bulterApi from "@souche2014/bulter-api";
const api = bulterApi({ partner: "IOParking" });
export default {
  data() {
    return {
      radio:null,
      submittype:false,
      upform:{
        parkId:'',
        orderId:'',
        orderType:'',
        gmF_NSRSBH:'',
        gmF_MC:'',
        gmF_DZDH:'',
        gmF_YHZH:'',
        gmF_LXFS:'',
        jshj:'',
        bz:''
      },
      khh:'',
      yhzh:'',
      apiBaseUrl: process.env.IOParking.API_HOST //请求头
    };
  },
  components: { Empty },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    this.upform.jshj = options.jg/100;
    this.upform.orderId = options.id;
    this.upform.orderType = options.type;
    this.upform.parkId = options.parkid;
  
    this.wx_login();
  },
  onPullDownRefresh() {
    
  },
  onReachBottom() {
 
  },
  methods: {
    wx_login() {
      api
        .getUserInfo({
          scope: "all"
        })
        .then(resp => {
          if (resp.success) {
            console.log("authCode is：", resp.data);
            if (resp.data == "mock") {
              setGlobalData("openId", "oRPxj5TQ040-YRuv0v7lhRvMyRB0");
              setGlobalData("mobile", "13021250990");
              this.initData();
              return;
            }
            this.getInfos(resp.data);
          } else {
            console.log("err is ", resp.message, "err code is ", resp.code);
          }
        });
    },
    getInfos(authCode) {
      let that = this;
      Taro.request({
        url: this.apiBaseUrl + "/weixin/souchegetuser?authCode=" + authCode,
        data: {},
        header: {
          "content-type": "application/json" // 默认值
        },
        method: "GET",
        success(res) {
          console.log(res.data);
          if (res.data.isSuccess) {
            var userOpenId = res.data.data.openId;
            setGlobalData("openId", userOpenId);
            setGlobalData("mobile", res.data.data.phoneNum);
          
          }
        }
      });
    },
    initData() {
     
    },
    radioChange(e){
      console.log(e.detail.value);
      this.radio = e.detail.value
    },

  

    onSubimt(){
      let that = this;
      that.upform.gmF_YHZH = that.khh + that.yhzh;
      let usedata = {
        parkId: that.upform.parkId,
        orderId: that.upform.orderId,
        orderType: that.upform.orderType,
        gmF_NSRSBH: that.upform.gmF_NSRSBH,
        gmF_MC: that.upform.gmF_MC,
        gmF_DZDH: that.upform.gmF_DZDH,
        gmF_YHZH: that.upform.gmF_YHZH,
        gmF_LXFS: that.upform.gmF_LXFS,
        jshj: that.upform.jshj,
        bz: that.upform.bz,
        mobile: getGlobalData("mobile")||"",
        wxOpenId: getGlobalData("openId")
      }
      
      console.log(usedata);
      if(!that.radio){
        Taro.showToast({
            title: "请选择类型",
            icon: "error",
            duration: 1000,
            mask: true
          });
        return
      }
      if(!usedata.gmF_MC){
        Taro.showToast({
            title: "请填写抬头",
            icon: "error",
            duration: 1000,
            mask: true
          });
        return
      }
      if(!usedata.gmF_NSRSBH && that.radio == 1){
        Taro.showToast({
            title: "请填写税号",
            icon: "error",
            duration: 1000,
            mask: true
          });
        return
      }
      if(that.radio == 2){
        usedata.gmF_NSRSBH = '';
      }
      Taro.showLoading({
        title: "提交中",
        mask: true
      });
      let base = this.apiBaseUrl;
      Taro.request({
        url: base + "/wxinvoice/create",
        data: usedata,
        header: {
          "content-type": "application/json" // 默认值
        },
        method: "POST",
        success(res) {
          Taro.hideLoading();
          console.log(res);
          if (res.data.isSuccess) {
            Taro.navigateBack({
              delta:1
            })
          } else {
            Taro.showToast({
              title: res.data.message,
              icon: "none",
              duration: 1000,
              mask: true
            });
          }
        }
      });
    }
    
   
  }
};
</script>